$("body").on('click','.receive-btn',function () {
    var couponId = $(this).data('coupon');
    couponReceive(couponId);
});

var receiveMap = {};
/**
 * 领取优惠券
 * @param couponId 优惠券ID
 */
function couponReceive(couponId){
    var _receive = receiveMap[couponId];
    if(_receive ==true){
        return;
    }
    isLogin(function () {
        $.ajax({
            url: baseloaction + '/web/uc/op/couponReceive',
            type:'post',
            data:{
                'couponIds':(couponId+'')
            },
            success:function (result) {
                icModale(0,'',result.message);
                if(result.success==true){
                    //设置成已领取
                    receiveMap[couponId] = true;
                }
            },
            error:function (error) {
                icModale(0,'',"系统错误，领取优惠券失败");
            }
        });
    });
}

/**
 * 获取课程可以使用的活动
 */
function courseInfoActivity(price) {
    $.ajax({
        url: baseloaction +'/web/activity/getCourseInfoActivity',
        type:'post',
        dataType:'json',
        data:{
            'price':price
        },
        success:function (result) {
            if(result.success==true){
                var list = result.entity;
                if(list !=null && list.length >0){
                    var context = '';
                    $.each(list,function (index,activity) {
                        if(index < list.length -1){
                            context += activity.activityName +'/';
                        }else{
                            context += activity.activityName;
                        }
                    });
                    $("#activity-context-box").text(context);
                }
            }
        }
    });
}

/**
 * 获取课程可领取的优惠券列表
 * @param _courseId 课程ID
 */
function getCourseCouponList(_courseId) {
    $.ajax({
        url: baseloaction +'/web/course/getCourseCouponList',
        type:'post',
        data:{
            'courseId':_courseId
        },
        success:function (result) {
            if(result.success==true){
                var list = result.entity;
                if(list !=null && list.length > 0){
                    var context = '';
                    $.each(list,function (index,coupon) {
                        context +='<li>';
                        context +='<div class="u-coupon__wrap">';
                        if(coupon.type==1){
                            context +='<section class="u-coupon-left u-coupon-zkq">';
                        }else{
                            context +='<section class="u-coupon-left u-coupon-djq">';
                        }
                        context +='<div class="text-center pt10 pb10 pl10 pr10">';
                        context +='<section class="u-coupon-num mt10">';
                        context +='<strong class="fs30 c-fff">'+coupon.limitAtion+'</strong>';
                        if(coupon.type==1){
                            context +='<span class="fs18 c-fff">折</span>';
                        }else{
                            context +='<span class="fs18 c-fff">￥</span>';
                        }
                        context +='</section>';
                        context +='<section class="hLh30">';
                        context +='<span class="c-fff fs12" title="【满'+coupon.minAmount+'元可用】">【满'+coupon.minAmount+'元可用】</span>';
                        context +='</section>';
                        context +='</div></section>';
                        context +='<section class="u-coupon-cont">';
                        context +='<div class="ml20 mr10 pt10 pb10">';
                        if(coupon.type==1){
                            context +='<h4 class="pt10"><span class="fs20 c-333">折扣券</span></h4>';
                        }else{
                            context +='<h4 class="pt10"><span class="fs20 c-333">满减券</span></h4>';
                        }
                        context +='<section class="hLh30 clearfix">';
                        if(coupon.receive==true){
                            context +='<button class="btn btn-danger btn-sm btn-rounded btn-outline pull-right" disabled type="button">已领取</button>';
                        }else{
                            context +='<button class="btn btn-danger btn-sm btn-rounded btn-outline pull-right receive-btn" data-coupon="'+coupon.id+'" type="button">领取</button>';
                        }
                        context +='<span class="c-999">';
                        if(coupon.timeType==1){
                            context +='<small class="c-999">有效期至：'+coupon.endTime+'</small>';
                        }else{
                            context +='<small class="c-999">有效期至：永久</small>';
                        }

                        context +='</span></section></div><div class="u-coupon-line_wrap"></div>';
                        context +='</section></div></li>';
                    });
                    $("#course-coupon-list").html(context);
                }else{
                    $("#show-receive-btn").parent().removeClass("open");
                    icModale(0,'','没有可领取的优惠券');
                }
            }else{
                icModale(0,'',result.message);
            }
        },
        error:function (error) {
            icModale(0,'','系统错误，获取优惠券列表失败');
        }
    });
}